package com.example.exchange.controller.exchange;


import com.example.exchange.common.CommonResponse;
import com.example.exchange.service.exchange.IExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

@Controller
@RequestMapping(value = "")
public class ExchangeController {
  @Autowired
  private IExchangeService exchangeService;


  @RequestMapping(value = {"/realtime"})
  @ResponseBody
  public Mono<Object> realtime(@RequestParam(required = false, value = "lng") String lng, @RequestParam(required = false, value = "lat") String lat) {
    CommonResponse response = CommonResponse.createCommonResponse();
    if (!StringUtils.hasText(lng)) {
      return Mono.just(response.success("lng must not be empty!!!"));
    }
    if (!StringUtils.hasText(lat)) {
      return Mono.just(response.success("lat must not be empty!!!"));
    }
    String location = lng + "," + lat;
    return exchangeService.realtime(location);
  }


  @RequestMapping(value = {"/minutely"})
  @ResponseBody
  public Object minutely(@RequestParam(required = false, value = "lng") String lng, @RequestParam(required = false, value = "lat") String lat) {
    CommonResponse response = CommonResponse.createCommonResponse();
    if (!StringUtils.hasText(lng)) {
      return Mono.just(response.success("lng must not be empty!!!"));
    }
    if (!StringUtils.hasText(lat)) {
      return Mono.just(response.success("lat must not be empty!!!"));
    }
    String location = lng + "," + lat;
    return exchangeService.minutely(location);
  }


  @RequestMapping(value = {"/weather"})
  @ResponseBody
  public Object weather(@RequestParam(required = false, value = "lng") String lng,
                        @RequestParam(required = false, value = "lat") String lat,
                        @RequestParam(required = false, value = "dailysteps", defaultValue = "1") Integer dailysteps,
                        @RequestParam(required = false, value = "hourlysteps", defaultValue = "1") Integer hourlysteps) {
    CommonResponse response = CommonResponse.createCommonResponse();
    if (!StringUtils.hasText(lng)) {
      return Mono.just(response.success("lng must not be empty!!!"));
    }
    if (!StringUtils.hasText(lat)) {
      return Mono.just(response.success("lat must not be empty!!!"));
    }
    String location = lng + "," + lat;
    return exchangeService.weather(location, dailysteps, hourlysteps);
  }
}
